﻿using System.Diagnostics;
using System.Windows.Media;

namespace WpfApp1.Models
{
    /// <summary>
    /// 连接线
    /// </summary>
    public class LinkBase : SharpBase
    {
        public LinkBase(PortBase sourcePort, PortBase targetPort)
        {
            SourcePort = sourcePort;
            TargetPort = targetPort;

            SourcePort.PropertyChanged += Port_PropertyChanged;
            TargetPort.PropertyChanged += Port_PropertyChanged;
            Update();
        }

        /// <summary>
        /// 两个连接点之间的线
        /// </summary>
        private Geometry _geometry;
        public Geometry Geometry { get => _geometry; set => SetProperty(ref _geometry, value); }

        /// <summary>
        /// 起始连接点
        /// </summary>
        private PortBase _sourcePort;
        public PortBase SourcePort { get => _sourcePort; set => SetProperty(ref _sourcePort, value); }

        /// <summary>
        /// 终止连接点
        /// </summary>
        private PortBase _targetPort;
        public PortBase TargetPort { get => _targetPort; set => SetProperty(ref _targetPort, value); }

        private void Port_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Debug.WriteLine($"LinkBasePort_PropertyChanged--{DateTime.Now.ToString("yyyyMMdd-HHmmssfff")}");
            Update();
        }

        public void Update()
        {
            var geometry = new PathGeometry();
            var figure = new PathFigure { StartPoint = SourcePort.Position };
            geometry.Figures.Add(figure);

            LineSegment arc = new LineSegment(TargetPort.Position, true) { IsSmoothJoin = true };
            geometry.Figures[0].Segments.Add(arc);
            Geometry = geometry;
            Left = Geometry.Bounds.Left;
            Top = Geometry.Bounds.Top;
            Width = Geometry.Bounds.Width;
            Height = Geometry.Bounds.Height;
        }
    }
}
